﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace WQ.Common
{
    public class GetHttpContent
    {
        public string GetWebContent(string url, string Encoding)
        {
            WebClient wb = new WebClient();
            byte[] pagedata;
            try
            {
                pagedata = wb.DownloadData(@url);
            }
            catch
            {
                //Log.RecordLog("天气网页打开失败！");
                return "";
            }
            return byteEncoding(pagedata, Encoding);
        }

        private string byteEncoding(byte[] pagedata, string Encoding)
        {
            switch (Encoding)
            {
                case "ASCII": return System.Text.Encoding.ASCII.GetString(pagedata);
                case "BigEndianUnicode": return System.Text.Encoding.BigEndianUnicode.GetString(pagedata);
                case "Default": return System.Text.Encoding.Default.GetString(pagedata);
                case "Unicode": return System.Text.Encoding.Unicode.GetString(pagedata);
                case "UTF32": return System.Text.Encoding.UTF32.GetString(pagedata);
                case "UTF7": return System.Text.Encoding.UTF7.GetString(pagedata);
                case "UTF8": return System.Text.Encoding.UTF8.GetString(pagedata);
                default: return "";
            }
        }
        public string Replace(string input, string replace, string strReg)
        {
            return Regex.Replace(input, strReg, replace);
        }
        public string CutString(string input, string left, string right)
        {
            int leftindex = 0;
            int rightindex = 0;

            if (left == "")
            {
                leftindex = 0;
            }
            else
            {
                leftindex = input.IndexOf(left);
            }
            if (right == "")
            {
                return input = input.Remove(leftindex);
            }
            else
            {
                rightindex = input.IndexOf(right);
            }
            return input.Remove(leftindex, rightindex - leftindex + 1);
        }
        public string SubString(string input, string left, string right)
        {
            int leftindex = 0;
            int rightindex = 0;

            if (left == "")
            {
                leftindex = 0;
            }
            else
            {
                leftindex = input.IndexOf(left);
            }
            if (right == "")
            {
                return input = input.Substring(leftindex);
            }
            else
            {
                rightindex = input.IndexOf(right);
            }
            input = input.Substring(leftindex, rightindex);
            return input;
        }
        public string PlusString(string left, string right)
        {
            return left + right;
        }
        public string GetContent(string strReg, string html)
        {
            Regex reg = new Regex(strReg, RegexOptions.Singleline);
            MatchCollection mc = reg.Matches(html);
            if (mc.Count > 0)
            {
                return mc[0].Groups["content"].Value;
            }
            return "";
        }
        public string GetWebContent2(string url, string Encoding)
        {
            Uri uri = new Uri(url);
            WebRequest wreq = WebRequest.Create(uri);
            HttpWebResponse wresp;
            try
            {
                wreq.Timeout = 30000;
                wresp = (HttpWebResponse)wreq.GetResponse();
            }
            catch
            {
                return "";
            }

            Stream s = wresp.GetResponseStream();
            StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding(Encoding));//,System.Text.Encoding.GetEncoding("GB2312"));
            return objReader.ReadToEnd();
        }
    }
}
